package com.example.myapplication.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.bean.User;
import com.example.myapplication.service.LoginService;
import com.example.myapplication.ui.component.CustomToast;
import com.example.myapplication.ui.mainframe.MainPageActivity;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "loginActivity";
    private LoginService loginService;
    private Button loginBtn;
    private EditText u_name;
    private EditText u_password;
    private CustomToast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        loginBtn = findViewById(R.id.loginButton);
        u_name = findViewById(R.id.username);
        u_password = findViewById(R.id.password);
        loginBtn.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();
        LoginActivity.this.finish();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.loginButton) {
            if (login()) {
                toastMessage("登录成功");
                Intent intent = new Intent(this, MainPageActivity.class);
                intent.putExtra("name", u_name.getText()
                .toString());
                startActivity(intent);
            } else {
                toastMessage("用户名与密码不匹配");
            }
        }
    }

    /**
     * 自定义toast显示信息
     */
    private void toastMessage(String content) {
        if (toast != null) {
            toast.hide();
        }
        toast = new CustomToast(LoginActivity.this,
                (ViewGroup) this.findViewById(R.id.toast_custom_parent));
        toast.show(content, 1000);
    }

    /**
     * 判断用户输入的用户名或者密码是否为空
     *
     * @param username 用户名
     * @param password 密码
     */

    public boolean isInputNone(String username, String password) {
        //"".equals(A)，防止nullPointerException。
        if ("".equals(username) || "".equals(password)) {
            toastMessage("用户名或密码为空");
            return true;
        }
        return false;
    }

    /**
     * 登录处理
     */
    public boolean login() {
        String username = u_name.getText().toString();
        String password = u_password.getText().toString();
//        // 测试用
//        if (isInputNone(username, password)) {
//            return true;
//        }
        LoginService loginService = new LoginService();
        User loginUser = loginService.getResponseUserInfo(username);

        if (loginUser != null) {
            return loginUser.getPassword().equals(password);
        } else {
            return false;
        }
    }
}